﻿#include "StdAfx.h"
#include "image.h"

image::image(void)
{
	this->init_var();
}
image::image(SDL_Surface * input_surface, SDL_Surface *screen)
{
	this->init_var();
	this->load(input_surface);
	this->main_screen = screen;
}
image::image(std::string filename, SDL_Surface *screen)
{
	this->init_var();
	this->load(filename);
	this->main_screen = screen;
}
image::image(std::string filename,SDL_Surface *screen, int r, int g, int b)
{
	this->init_var();
	this->load(filename);
	this->main_screen = screen;
	this->colorkey(r, g, b);
}
image::image(std::string filename,SDL_Surface *screen, int r, int g, int b, int sprite_count)
{
	this->init_var();
	this->load(filename);
	this->main_screen = screen;
	this->colorkey(r, g, b);
	this->sprite = sprite_count;
}
void image::init_var()
{
	this->img_surface = NULL;
	this->offset.h = 0;
	this->offset.w = 0;
	this->offset.x = 0;
	this->offset.y = 0;
	this->set_xy.x = 0;
	this->set_xy.y = 0;
	this->set_xy.w = 0;
	this->set_xy.h = 0;
	this->sprite = 1;
	this->flag_img = false;
}
SDL_Surface *image::load_image(std::string filename)
{
	//로드한 이미지를 잠시 저장할 서피스
	SDL_Surface *loadedImage=NULL;

	//다양한 용도로 사용할 서피스
	SDL_Surface *optimizedImage = NULL;

	//이미지 불러오기
	loadedImage = IMG_Load( filename.c_str());

	//이미지 로드가정상적으로 로드 되었다면
	if(loadedImage != NULL)
	{
		// Create an optimized image
		optimizedImage = SDL_DisplayFormat(loadedImage);

		// 필요 없는 이미지 해제
		SDL_FreeSurface(loadedImage);
	}

	//값 반환 optimized image
	return optimizedImage;
}

bool image::load(std::string filename)
{
	this->img_surface = this->load_image(filename);
	if(this->img_surface == NULL)
		this->e_msg("이미지 열기 실패!", filename);
	else
		this->flag_img = true;

	//이미지 정보 설정
	this->offset.x = 0;
	this->offset.y = 0;
	this->offset.w = this->img_surface->w;
	this->offset.h = this->img_surface->h;

	this->set_xy.x = 0;
	this->set_xy.y = 0;
	this->set_xy.w = this->img_surface->w;
	this->set_xy.h = this->img_surface->h;

	return flag_img;
}
bool image::load(SDL_Surface * input_surface)
{
	this->img_surface = input_surface;
	if(this->img_surface == NULL)
		this->e_msg("이미지 열기 실패!", "input_surface 에러");
	else
		this->flag_img = true;

	//이미지 정보 설정
	this->offset.x = 0;
	this->offset.y = 0;
	this->offset.w = this->img_surface->w;
	this->offset.h = this->img_surface->h;

	this->set_xy.x = 0;
	this->set_xy.y = 0;
	this->set_xy.w = this->img_surface->w;
	this->set_xy.h = this->img_surface->h;

	return flag_img;
}
int image::colorkey(int r, int g, int b)
{
	Uint32 colorkey = SDL_MapRGB(this->img_surface->format, r, g, b);
	return SDL_SetColorKey(this->img_surface, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey);
}
image::~image(void)
{
	if(flag_img == true)
		SDL_FreeSurface(this->img_surface);
}
int image::set(int in_x, int in_y, int in_sel)
{
	this->set_xy.x = in_x;
	this->set_xy.y = in_y;
	
	this->offset.x = in_sel* ( this->img_surface->w / this->sprite);
	this->offset.x = in_sel* 22;
	this->offset.w = this->img_surface->w/this->sprite;
	int return_int = SDL_UpperBlit(this->img_surface,&this->offset,this->main_screen,&set_xy); 

	//SDL_UpperBlit 에서 set_xy값을 바꾸기 때문에 재설정
	this->set_xy.x = in_x;
	this->set_xy.y = in_y;

	return return_int;
}

int image::set(SDL_Rect offset, int in_x, int in_y)
{
	SDL_Rect set_xy;
	set_xy.x = in_x;
	set_xy.y = in_y;
	int return_int = SDL_UpperBlit(this->img_surface,&offset,this->main_screen,&set_xy); 

	return return_int;
}

int image::sprite_change(int sel, int range, int input)
{
	static int count = 0;
	int output = input;
	if(count % 5 == 0)
	{
		if(input < range+sel-1 && input >= sel)
			output=  input+1;
		else
			output =  sel;
	}
	count = (count + 1) %5;

	return output;
}